package com.xm.myapplication.MVP.A.View;

import android.content.Intent;
import android.os.Bundle;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.umeng.socialize.UMShareAPI;
import com.xm.httpapi.BaseView.BaseActivity;
import com.xm.myapplication.R;
import com.xm.myapplication.View.TabEntity;

import java.util.ArrayList;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.ButterKnife;

public class IndexActivity extends BaseActivity {
    @BindView(R.id.vp)
    public ViewPager viewPager;
    @BindView(R.id.tl_bottom)
    public CommonTabLayout commonTabLayout;

    private String[] mTitles = {"工作台", "我的"};
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private int[] mIconUnselectIds = {R.mipmap.tab_sale_unselect, R.mipmap.tab_mine_unselect};
    private int[] mIconSelectIds = {R.mipmap.tab_sale_select, R.mipmap.tab_mine_select};
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private HomeMVPFragment homeFragment = new HomeMVPFragment();
    private MineFragment mineFragment = new MineFragment();

    @Override
    protected int getLayoutId() {
        return R.layout.index;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        ButterKnife.bind(this);
        mFragments.add(homeFragment);
        mFragments.add(mineFragment);
        viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }
        commonTabLayout.setTabData(mTabEntities);
        commonTabLayout.setOnTabSelectListener(new com.flyco.tablayout.listener.OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                commonTabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected String showTitle() {
        return "标题";
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //友盟qq分享需要
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }
}
